#include <iostream>
#include <vector>
using namespace std;

vector<string> splitstring(const string& str, const string& delimiter)
{
	vector<string> result;
	unsigned int begin = 0;
	bool ever_matched = false;
	int i = 0;
	int j = 0;
	while(i + delimiter.length() - 1 < str.length())
	{
		for(j = 0; j < delimiter.length(); j++)
		{
			if(str[i+j] != delimiter[j])
				break;
		}
		if(j == delimiter.length())
		{
			ever_matched = true;
			if(begin <= i-1)
				result.push_back(string(str, begin, i-begin));
			i += delimiter.length();
			begin = i;	
		}
		else
			i++;
	}
	if(begin < str.length() && ever_matched)
		result.push_back(string(str, begin, str.length()-begin));


	return result;
}

vector<string> splitstring_r(const string& str, const string& delimiter)
{
	if(str.length() < delimiter.length())
	{
		vector<string> result;
		if(str.length())
			result.push_back(str);
		return result;
	}		
	for(int j=0; j<str.length()-delimiter.length()+1; j++)
	{
		int i;
		for(i=0; i<delimiter.length(); i++)
		{
			if(str[j+i] != delimiter[i])	
				break;
		}
		if(i == delimiter.length())
		{
			vector<string> result = splitstring_r(string(str, j+delimiter.length(), str.length()-j-delimiter.length()), delimiter);
			if(j)
				result.push_back(string(str, 0, j));
				return result;
		}
	}
	vector<string> result;
	result.push_back(str);
	return result;
}
int main()
{
	vector<string> strs;
	strs.push_back("abxxacxxadxx"); 
	strs.push_back("abcdef");
	strs.push_back("xxabcedf");
	strs.push_back("xx");
	strs.push_back("abcxx");
	strs.push_back("a");
	strs.push_back("xxxxabxxxxacxxxx");
	strs.push_back("xxxx");
	strs.push_back("xxxxx");
	string d = "xx";
	
	for(int i = 0; i < strs.size(); i++)
	{
		string s = strs[i];
		cout << s << ":" << endl;
		vector<string> re = splitstring_r(s, d);
		for(vector<string>::iterator it = re.begin(); it !=re.end(); it++)
			cout << *it << " ";
		cout << endl;
	}
	return 0;
}
